home *** CD-ROM | disk | FTP | other *** search
- /* Roll pitch yaw demonstration */
- /* by Karl Lager */
- /* 11/27/95 */
-
-
- #include <math.h>
- #include <stdio.h>
-
- /* equatorial to alt-az: */
- /* altitude = arcsin(sin(dec)*sin(lat) + cos(dec)*cos(lat)*cos(ha)) */
- /* azimuth = arccos( (sin(dec) - sin(lat)*sin(alt))/(cos(lat)*cos(alt)) )
-
- /* alt-az to equatorial */
- /* dec = arcsin(sin(alt)*sin(lat) + cos(alt)*cos(lat)*cos(az)) */
- /* ha = arccos( (sin(alt) - sin(lat)*sin(dec))/(cos(lat)*cos(dec)) ) */
-
-
-
- const double rad2deg = 180/M_PI;
- const double deg2rad = M_PI/180;
-
- void alt_az_to_eq(double alt, double az,
- double *dec, double *ha,
- double lat);
-
- void orient_ship(double *x, double *y, double *z,
- double pitch, double yaw,double roll)
- /* all angles are in radians */
- {double ha, ha2,dec, lat, lon,alt,az;
- // correct for reverse yaw angle(turn right = +)
- yaw = -yaw; // now all inputs are right hand angles
- /*// locate nose of ship*/
- lat = *x; lon = *y;
- /*// locate axis of rotation*/
- if (pitch == 0 && yaw == 0) {*z += roll; return;}
- alt = atan2(roll,sqrt(pitch*pitch+yaw*yaw));
- az = *z+ atan2(pitch,yaw);
- *z -= az;
- alt_az_to_eq(alt,az,&dec,&ha,lat);
- /*//locate az of ship*/
- lat = dec;
- alt_az_to_eq(*x,-ha,(&alt),&az,lat);
- az += sqrt(roll*roll+pitch*pitch+yaw*yaw);
- alt_az_to_eq(alt,az,&dec,&ha2,lat);
-
- alt_az_to_eq(lat,ha2,(&alt),&az,dec);
- *x = dec; while (*x<0) *x += M_PI*2; while (*x>=M_PI*2) *x -= M_PI*2;
- *y = lon-ha-ha2; while(*y<0) *y += M_PI*2; while (*y>=M_PI*2) *y -= M_PI*2;
- *z -= az; while (*z<0) *z += M_PI*2; while (*z>=M_PI*2) *z -= M_PI*2;
- }
-
-
- void alt_az_to_eq(double alt, double az,
- double *dec, double *ha,
- double lat)
- {
- double sin_dec,cos_ha,sinalt,sinlat,coslat;
- sinalt = sin(alt); sinlat = sin(lat); coslat = cos(lat);
- sin_dec = sinalt*sinlat + cos(alt)*coslat*cos(az);
- *dec = asin(sin_dec);
- if (sin_dec == 1) cos_ha = 1 ; else
- cos_ha = (sinalt - sinlat*sin_dec)/(coslat*cos(*dec)) ;
- if (cos_ha > 1)
- cos_ha = 1;
- if (cos_ha < -1)
- cos_ha = -1;
-
- *ha = acos(cos_ha);
- if (sin(az) > 0) *ha = 2*M_PI - *ha;
- }
-
-
- //_____________________________________________________________________
-
-
-
- void main()
- {double roll,pitch,yaw,x,y,z;
- int i;
-
- printf("enter pitch, yaw, roll=0 0 0 to exit.\n");
- printf("enter xrot,yrot,zrot, pitch,yaw,roll :");
- scanf("%lf%lf%lf%lf%lf%lf",&x,&y,&z,&pitch,&yaw,&roll);
- while (roll || pitch || yaw)
- {
- x *= deg2rad;
- y *= deg2rad;
- z *= deg2rad;
- roll *= deg2rad;
- pitch *= deg2rad;
- yaw *= deg2rad;
-
- orient_ship(&x,&y,&z,pitch,yaw,roll);
-
- x *= rad2deg;
- y *= rad2deg;
- z *= rad2deg;
- printf(" xrot = %lf, yrot = %lf, zrot = %lf\n",x,y,z);
- printf("enter pitch, yaw, roll (0, 0, 0 to exit.)\n");
- scanf("%lf%lf%lf",&pitch,&yaw,&roll);
-
- }
- }
-
- /*
- note- The co-ordinate system I use here is:
- with xrot = 0, yrot = 0, and zrot = 0
-
- x points right, y points up, and z points into the screen.
- +pitch pulls up on the stick, +yaw turns right, and +roll roll to the right.
- zrot, xrot, and yrot follow the directions of roll, pitch, and yaw.
- In other words, zrot is right handed, xrot is right handed, and yrot
- is lefthanded.
-
- This is a little bit oddball, since most texts tell you to pick one hand
- and stick with it. To change the handednes of a rotation variable,
- the simplest way is to reverse the sign of the variable at the beginning
- and end of the orientship procedure, the way I did with yaw.
-
-
- By the way, if you want to move your object in the direction it is
- pointing, you will need to convert the rotation angles to vectors.
-
-
- x += speed*cos(xrot)*sin(yrot));
- y += speed*sin(xrot));
- z += speed*cos(xrot)*cos(yrot);
-
-
- */